<?php
function links_onLoad($db) {
	// Increase our counter and redirect user
	if (!empty($_GET['id']) && isset($_GET['url'])) {
		$db->update('links', array('hits' => new Zend_Db_Expr('(hits+1)')), 'id = ' . $db->quote($_GET['id']));
		header("Location: " . $db->fetchOne("SELECT url FROM `links` WHERE id = " . $db->quote($_GET['id'])));
		exit;
	}
	elseif ($GLOBALS['authNamespace']->admin && !empty($_POST['id']) && isset($_POST['linkssubmit'])) {
		$db->update('links', array('title' => $_POST['title'], 'url' => $_POST['url'], 'desc' => $_POST['desc']), 'id = ' . $db->quote($_POST['id']));
		$GLOBALS['logger']->log('Successful update of link id ' . $_POST['id'] , Zend_Log::NOTICE);
	}
	elseif ($GLOBALS['authNamespace']->admin && isset($_POST['linkssubmit'])) {
		$db->insert('links', array('title' => $_POST['title'], 'url' => $_POST['url'], 'desc' => $_POST['desc']));
		$GLOBALS['logger']->log('Successful add of link id ' . $db->lastInsertId() , Zend_Log::NOTICE);
		header("Location: index.php?mod=links");
		exit;
	}
	elseif ($GLOBALS['authNamespace']->admin && !empty($_GET['id']) && $_GET['action'] == 'delete') {
		$db->delete('links', 'id = '.$db->quote($_GET['id']));
		$GLOBALS['logger']->log('Successful delete of link id ' . $_GET['id'] , Zend_Log::NOTICE);
	}
}

function links_main($db, &$view, $translate) {
	global $config;
	if ($GLOBALS['authNamespace']->admin && isset($_GET['action']) && ($_GET['action'] == 'edit' || $_GET['action'] == 'new')) {
		$data = $db->fetchRow('SELECT * FROM `links` WHERE id = ' . $db->quote($_GET['id']));	
		$view->assign('data', $data);
		$view->display('file:../../../modules/links/links_form.tpl');
	}
	else {
		$data = $db->fetchAll("SELECT * FROM `links`");
		$view->assign('links', $data);
		$view->display('links.tpl');
	}
}
?>
